﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HZ.Supervise.Web.Admin
{
    public partial class MessageGet : BasePage
    {
        HZ.Supervise.IBLL.IExecuteContext im = HZ.Supervise.BLLFactory.ExecuteContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rblMessageType_SelectedIndexChanged(sender, e);
            }
        }

        /// <summary>
        /// 绑定所有发出的消息
        /// </summary>
        private void SetBind()
        {
            int temp; 
            IList<Model.View.Message> lstMessage = new List<Model.View.Message>();
            switch (rblMessageType.SelectedValue)
            {
                case "M1":
                    lstMessage = im.GetMessage(new Model.Query.QMessage() { SortFields = "SendTime DESC", Reciever=CurrentAccount.GetUser().Org.OrgID }, 1, 10, out temp);
                    break;
                case "M2":
                    lstMessage = im.GetMessageSent(new Model.Query.QMessage() { SortFields = "SendTime DESC", Sender=CurrentAccount.GetUser().Org.OrgID }, 1, 10, out temp);
                    break;
                default:
                    break;
            }
            gvwMsgList.DataSource = lstMessage;
            gvwMsgList.DataBind(); 
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            rblMessageType_SelectedIndexChanged(sender, e);
        }

        /// <summary>
        /// 绑定信息的状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwMsgList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (rblMessageType.SelectedValue)
            {
                case "M1":
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        Literal ltrStatus = e.Row.FindControl("ltrStatus") as Literal;
                        if (ltrStatus.Text == "0")
                        {
                            ltrStatus.Text = "未阅读";
                        }
                        else if (ltrStatus.Text == "1")
                        {
                            ltrStatus.Text = "已阅读";
                        }
                        else if (ltrStatus.Text == "2")
                        {
                            ltrStatus.Text = "已回复";
                        }
                    }
                    break;
                case "M2":
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        Literal ltrStatus = e.Row.FindControl("ltrStatus") as Literal;
                        ltrStatus.Text = "成功发送"; 
                    }
                    break;
            } 
        }

        protected void rblMessageType_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetBind();
        }
    }
}